package com.haotian.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.log4j.Logger;

public class _FileUtils {

	private _FileUtils() {
	}

	private static final Logger log = Logger.getLogger(_FileUtils.class);


	/**
	 * B方法 使用FileWriter full文件名称和路径 内容 是否追加
	 * 
	 * @param fileName
	 * @param content
	 */
	public synchronized static void appendMethodB(String fileName, String content, boolean append) {
		try {
			FileWriter writer=new FileWriter(fileName, append);
			writer.write(content);
			writer.write(System.lineSeparator());// 写入换行
			writer.close();
		} catch (IOException e) {
			log.error("content:" + content, e);
		}
	}

	/**
	 * 无二级文件夹
	 * <p>
	 * </p>
	 * 
	 * @param msg
	 * @param path
	 * @param fileName
	 */
	public static void saveMessageToFile(String msg, String path, String fileName) {
		try {
			StringBuffer savePath = new StringBuffer(path);
			// 处理一级文件夹
			File saveDirFile = new File(savePath.toString());
			if (!saveDirFile.exists()) {
				saveDirFile.mkdirs();
			}
			savePath.append("/");
			// 处理文件名称
			savePath.append(fileName);
			_FileUtils.appendMethodB(savePath.toString(), msg, true);
			savePath = null;
		} catch (Exception e) {
			log.error("", e);
		}
	}

	/**
	 * 保存数据到文件内 追加形式 已日志为目录
	 * 
	 * @param msDto
	 */
	public static void saveMessageToDateDirFile(String msg, String path, String fileName) {
		try {
			StringBuffer savePath = new StringBuffer(path);
			// 处理一级文件夹
			File saveDirFile = new File(savePath.toString());
			if (!saveDirFile.exists()) {
				saveDirFile.mkdirs();
			}
			// 处理二级文件夹 使用日期分类
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String ymd = sdf.format(new Date());

			savePath.append("/");
			savePath.append(ymd);
			savePath.append("/");

			File dirFile = new File(savePath.toString());
			if (!dirFile.exists()) {
				dirFile.mkdirs();
			}

			// 处理文件名称
			savePath.append(fileName);
			_FileUtils.appendMethodB(savePath.toString(), msg, true);
		} catch (Exception e) {
			log.error("", e);
		}
	}

	/**
	 * 获取指定路径下的某一个文件的全部内容
	 * 
	 * @param fileName
	 *            完整的路径+文件名称全程包含扩展名
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static String getFileToString(String fileName) throws FileNotFoundException, IOException {

		try (FileInputStream fis = new FileInputStream(new File(fileName));

				InputStreamReader isr = new InputStreamReader(fis, "utf-8");

				BufferedReader br = new BufferedReader(isr);) {

			String tmp = null;
			StringBuilder sb = new StringBuilder();

			while ((tmp = br.readLine()) != null) {
				sb.append(tmp).append(System.lineSeparator());
			}
			return sb.toString();
		}

	}

	/**
	 * 快速获取一个 bufferedReader 对象
	 * 
	 * @param fileName
	 *            完整文件路径包括文件本身全称
	 * @param charset
	 *            文件编码
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static BufferedReader getFileBufferedReader(String fileName, String charset)
			throws FileNotFoundException, IOException {

		FileInputStream fis = new FileInputStream(new File(fileName));

		InputStreamReader isr = new InputStreamReader(fis, charset);

		BufferedReader br = new BufferedReader(isr);

		return br;
	}

	/**
	 * 获取 classloader
	 * 
	 * @return
	 */
	public static ClassLoader getClassLoader() {

		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
		if (classLoader == null) {
			classLoader = _FileUtils.class.getClassLoader();
		}

		return classLoader;
	}

	/**
	 * 递归处理文件 带参数
	 * 
	 * @param file
	 *            根文件
	 * @param service
	 *            处理服务接口 需要用户自行实现
	 */
	public static void RecursiveRun(File file, _FileService service, Object object) {

		if (file.isDirectory()) {
			File[] listFiles = file.listFiles();
			for (File f2 : listFiles) {
				RecursiveRun(f2, service, object);
			}
		} else {
			service.runRunnable(file, object);
		}

	}

	/**
	 * 递归处理文件 不带参数
	 * 
	 * @param file
	 *            根文件
	 * @param service
	 *            处理服务接口 需要用户自行实现
	 */
	public static void RecursiveRun(File file, _FileService service) {

		if (file.isDirectory()) {
			File[] listFiles = file.listFiles();
			for (File f2 : listFiles) {
				RecursiveRun(f2, service);
			}
		} else {
			service.runRunnable(file);
		}

	}

	/**
	 * 获取File的内容总行数
	 * <p>
	 * </p>
	 * 
	 * @return
	 * @throws IOException
	 */
	public static int getFileNumbers(File file) throws IOException {
		int count = 0;
		try (BufferedReader reader = new BufferedReader(new FileReader(file), 512);) {
			while (reader.readLine() != null) {
				count++;
			}
		}
		return count;
	}

}
